package com.apes.fn.scm.insuranceRules;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.apes.crm.business.model.InsuranceCompany;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.area.model.Area;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName : InsuranceRule
 * @Description : 保险规则
 * @Author : WSJ
 * @Date: 2020-04-07 10:50
 */
@Getter
@Setter
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InsuranceRule.CONST_TABLE_NAME)
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
public class InsuranceRule extends Model {
    public static final String CONST_TABLE_NAME = "insurance_rule";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "保险规则", padding = 4)
    @Comment("ID")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "insurance_Company_id",referencedColumnName = "id" ,nullable = false)
    @Comment("保险公司")
    private InsuranceCompany insuranceCompany;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "Company_id",referencedColumnName = "id" ,nullable = false)
    @Comment("销售公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("销售部门")
    private Dept dept;  //销售公司下面的销售部门

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "salesman_id",referencedColumnName = "id" )
    @Comment("业务员")
    private PartyRole salesman;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "province_id", referencedColumnName = "id")
    @Comment("省份")
    private Area province;

    @ManyToOne
    @JoinColumn(name = "city_id", referencedColumnName = "id")
    @Comment("城市")
    private Area city;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate;

    @Comment("有效标记")
    @Column(name = "valid",columnDefinition = ApesConst.FIELD_BOOLEAN_TRUE)
    private boolean valid = true;

    @Comment("默认标记")
    @Column(name = "default_Mark",columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    private boolean defaultMark = false;

    @Transient
    private String provinceAndCity;

    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="s_rule_dept",
            joinColumns={@JoinColumn(name="rule_id",referencedColumnName = "id")},
            inverseJoinColumns={@JoinColumn(name="dept_id",referencedColumnName = "id")})
    private List<Dept> damagePoint = new ArrayList<>();

    /**
      前端展示字段
     */
    @Transient
    private List<JSONObject> deptList;

}
